package org.dromara.canteen.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import org.dromara.canteen.domain.CanteenFood;
import org.dromara.canteen.domain.bo.CanteenFoodBo;
import org.dromara.canteen.domain.vo.CanteenFoodVo;
import org.dromara.canteen.service.ICanteenFoodService;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.core.enums.UserType;
import org.dromara.common.core.exception.ExcelException;
import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.tenant.helper.TenantHelper;
import org.dromara.school.domain.Campus;
import org.dromara.school.domain.Discipline;
import org.dromara.school.domain.Teacher;
import org.dromara.school.domain.bo.TeacherBo;
import org.dromara.school.domain.vo.TeacherVo;
import org.dromara.school.service.ICampusService;
import org.dromara.school.service.IDisciplineService;
import org.dromara.school.service.ITeacherService;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description //TODO
 * @Date 2025/6/16 15:53
 * @Author 郭建波
 **/
public class FoodImportExcelListener extends AnalysisEventListener<CanteenFoodVo> {
    private final ICanteenFoodService canteenFoodService;
    private ICampusService schoolCampusService;
    //定义存放数据的集合
    private List<CanteenFoodVo> list = new ArrayList<>();

    public FoodImportExcelListener(ICanteenFoodService canteenFoodService, ICampusService schoolCampusService) {
        this.canteenFoodService = canteenFoodService;
        this.schoolCampusService = schoolCampusService;
    }

    // 一行行读取内容
    @Override
    public void invoke(CanteenFoodVo canteenFoodVo, AnalysisContext analysisContext) {


        CanteenFoodVo getCanteenFood = this.checkNull(canteenFoodVo, analysisContext);



        //读取每行数据，对数据进行判断以及处理
        Long schoolCampusId = getCanteenFood.getSchoolCampusId();

        //根据 日期 校区 食材名称 食材重量判断是否存在
        this.checkHasFood(canteenFoodVo,analysisContext);

        CanteenFoodVo food = new CanteenFoodVo();
        BeanUtils.copyProperties(canteenFoodVo,food);
        food.setSchoolCampusId(schoolCampusId);
        list.add(food);
    }

    private void checkHasFood(CanteenFoodVo canteenFoodVo, AnalysisContext context) {

          CanteenFood food  = this.canteenFoodService.findFood(canteenFoodVo);
        if(food!=null){
            int row = context.readRowHolder().getRowIndex() + 1;
            throw new ExcelException(context.readSheetHolder().getSheetName() + "：第" + row + "行 " + "食材:" + food.getFoodName() + canteenFoodVo.getPurchaseDate()+" 已经存在该食材信息，请检查！");
        }

    }


    // 读取表头内容
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
    }

    // 读取完成之后的操作
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        list.forEach(food -> {
            CanteenFoodBo  canteenFoodBo = new CanteenFoodBo();
            canteenFoodBo.setFoodName(food.getFoodName());
            canteenFoodBo.setPurchaseDate(DateUtils.getDateString(food.getPurchaseDate()));
            canteenFoodBo.setUnit(food.getUnit());
            canteenFoodBo.setPrePurchaseQuantity(Long.valueOf(food.getPrePurchaseQuantity()));
            canteenFoodBo.setSchoolCampusId(food.getSchoolCampusId());
            this.canteenFoodService.insertByBo(canteenFoodBo);
        });


    }


    //判断每一行必填数据，返回校区id
    public CanteenFoodVo checkNull(CanteenFoodVo canteenFoodVo, AnalysisContext context) {
        int row = context.readRowHolder().getRowIndex() + 1;
        if(StringUtils.isEmpty(canteenFoodVo.getSchoolCampusName())||
            StringUtils.isEmpty(canteenFoodVo.getFoodName())||
            canteenFoodVo.getPurchaseDate()==null||
            StringUtils.isEmpty(canteenFoodVo.getPrePurchaseQuantity())){
            throw new ExcelException(context.readSheetHolder().getSheetName() + "：第" + row + "行  出现导入必须要的数据为空,请检查！");
        }

        String tenantId = TenantHelper.getTenantId();

        LoginUser loginUser = LoginHelper.getLoginUser();
        UserType userType = UserType.getUserType(loginUser.getUserType());

        if (StringUtils.isNull(tenantId)) {
            throw new ExcelException( "未匹配到租户id 请联系管理员");
        }

        Campus schoolCampus = new Campus();
        schoolCampus.setCampusName(canteenFoodVo.getSchoolCampusName());
        //如果不是系统管理员
        if(!userType.equals(UserType.SYS_USER)){
            schoolCampus.setTenantId(tenantId);
        }
        Campus getSchoolCampus = schoolCampusService.findSchoolCampus(schoolCampus);

        if (StringUtils.isNull(getSchoolCampus)) {
            throw new ExcelException(context.readSheetHolder().getSheetName() + "：第" + row + "行  导入的校区没有找到,请检查！");
        }
        canteenFoodVo.setSchoolCampusId(getSchoolCampus.getCampusId());
        return canteenFoodVo;
    }



}
